package com.qf.mpswaggershiro.user.controller;


import com.qf.mpswaggershiro.user.entity.Users;
import com.qf.mpswaggershiro.user.service.IUsersService;

import com.qf.mpswaggershiro.vo.RegistVo;
import com.qf.mpswaggershiro.vo.Vo;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * <p>
 * 用户  前端控制器
 * </p>
 *
 * @author cui
 * @since 2023-02-13
 */
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/user")
public class UsersController {
@Autowired
    IUsersService usersService;

    @GetMapping("/test")
    public String test(){
        return "test";
    }
    @GetMapping("/get/{id}")
    public Users getUser(@PathVariable("id") Integer id){

        Users users = usersService.getById(id);
        return users;
    }

    /**
     * 登录接口
     * @param username
     * @param password
     * @return
     */
  @GetMapping("/login")
    public Vo login(String username, String password){
       // UserVo userVo = new UserVo();
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        subject.login(usernamePasswordToken);
       //如何获得 数据库中数据对应的users对象呢
      Users users = (Users) subject.getPrincipal();
      //登录成功
      Vo success = Vo.ok(10000, "success");
      success.put("data",users);

      return success;


  }

    /**
     * 注册接口
     * @param registVo
     * @return
     * @throws Exception
     */
    @PostMapping("/regist")
//@Valid 参数校验 因为RegistVo中  加了@NotEmpty 非空注解
  public Vo regist(@Valid @RequestBody RegistVo registVo) throws Exception {

      //1.提供注册接口
      usersService.regist(registVo);
//5.返回数据额给页面
      return Vo.ok(10000);
  }






}
